<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var averageOfSubtree = function (root) {
      let ans = 0

      function dfs(node) {
        if (!node) {
          return [0, 0]
        }
        const [lc, lsum] = dfs(node.left)
        const [rc, rsum] = dfs(node.right)
        const sum = lsum + rsum + node.val
        const count = lc + rc + 1
        if (Math.floor(sum / count) === node.val) {
          ans++
        }
        return [count, sum]
      }

      dfs(root)
      return ans
    };
    console.log(averageOfSubtree(root = [4, 8, 5, 0, 1, null, 6]));
  </script>
</body>

</html>